package com.mew.chess.core.common.handler;

import cn.hutool.core.util.StrUtil;
import com.mew.common.dc.redis.GameRoomRedisService;
import com.mew.common.entity.config.Game;
import com.zebra.common.TmsServerType;
import com.zebra.remoting.config.TmsServerInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ChessServerStartService {


    @Autowired
    private GameRoomRedisService gameRoomRedisService;
//    @Autowired
//    private AucRedisService aucRedisService;


    public TmsServerInfo startBefore(String vfxs, int contains, TmsServerType gameServerType) throws Exception {

        if (contains == 1 && StringUtils.isEmpty(vfxs)){
            throw new Exception("contains =1 game-list is null config error");
        }

        if (contains == 0 && "0".equals(vfxs)){
            throw new Exception("contains =0 game-list=0 config error");
        }


        List<String> vfxList = !"0".equals(vfxs) ? new ArrayList<>(Arrays.asList(vfxs.split(","))) : new ArrayList<>();

        List<Game> allGame = gameRoomRedisService.findGameByType(gameServerType);
        if (null == allGame || allGame.isEmpty()){
            throw new Exception("Redis game is null");
        }

        List<Game> gameList= new ArrayList<>();

        if (contains==1 && vfxList.size()>0){
            for (String vfx : vfxList) {
                Game game = getGameByVfx(vfx, allGame);
                if (null == game){
                    throw new Exception("vfx "+ vfx +" not Game");
                }

                doStartBefore(game, gameList);
            }
        }else {
            for (Game game : allGame) {
                if (vfxList.contains(game.getGameVfx())){
                    continue;
                }
                doStartBefore(game, gameList);
            }
        }

        if (gameList.isEmpty()){
            throw new Exception("Not Config game vfxs");
        }
//        String games = StrUtil.join(",", gameVfx);
//        log.info("start game: {}", games);
//        return games;
        List<String> vfxAfter = gameList.stream().map(Game::getGameVfx).collect(Collectors.toList());
        List<Integer> gamdIds = gameList.stream().map(Game::getId).collect(Collectors.toList());
        String gamevfxs = StrUtil.join(",", vfxAfter) + ",";
        String gameIds = StrUtil.join(",", gamdIds) + ",";
        TmsServerInfo serverInfo = new TmsServerInfo();
        serverInfo.setTmsType(TmsServerType.Chess.code());
        serverInfo.setVfxs(gamevfxs);
        serverInfo.setGameIds(gameIds);
        log.info("start serverInfo: {} {}", serverInfo.toString());
        log.info("CG== {} ==",serverInfo.getVfxs());
        return serverInfo;
    }


    private void doStartBefore(Game game,  List<Game> gameList) throws Exception {
        ChessStart chessStart = InitializeStartMapping.getMapping(game.getGameVfx());
        if (null == chessStart){
            throw new Exception("vfx "+ game.getGameVfx() +" not ChessStart");
        }
        ChessEEHandler chessEEHandler = InitializeEEHandlerMapping.getMapping(game.getGameVfx());
        if (null == chessEEHandler){
            throw new Exception("vfx "+ game.getGameVfx() +" not ChessEEHandler");
        }
        chessStart.initializeGame(game.getId());
        gameList.add(game);
        GameInfoMananger.getInstance().put(game);
    }

    private Game getGameByVfx(String vfx, List<Game> gameList){
        return gameList.stream().filter(game -> vfx.equals(game.getGameVfx())).findAny().orElse(null);
    }

//    private void offLine(List<Integer> gids){
//        List<MewSession> sessions = aucRedisService.findOnlineByGid(gids);
//        if (null == sessions || sessions.isEmpty()){
//            return;
//        }
//        sessions.forEach(ses -> aucRedisService.offlineSession(ses.uid));
//    }

}
